/**
 * 提取时间轴的工具函数
 * */
// 时间转为 00:00
export function secondFormat(second, showNum = Number('2'), split = ':') {
  const time1 = 86400;
  const time2 = 3600;
  const time3 = 60;
  const days = Math.floor(second / time1);
  const hours = Math.floor((second % time1) / time2);
  const minutes = Math.floor(((second % time1) % time2) / time3);
  const seconds = Math.floor(((second % time1) % time2) % time3);
  const timeArr = [days, hours, minutes, seconds];
  return timeArr
    .slice(timeArr.findIndex((item, index) => item !== 0 || index === timeArr.length - showNum))
    .map(item => item.toString().padStart(Number('2'), '0'))
    .join(split);
}
// 生成uuid
export function generateUUID() {
  const NUM1 = 16;
  const NUM2 = 0x3;
  const NUM3 = 0x8;
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = (Math.random() * NUM1) | 0;
    const v = c === 'x' ? r : (r & NUM2) | NUM3;
    return v.toString(NUM1);
  });
}
// 判断两个元素是否相交,相等!=相交
export function isRangeOverlap(arr1, arr2) {
  let arr1Copy = arr1;
  let arr2Copy = arr2;
  // 确保两个数组的最小值都小于最大值
  if (arr1Copy[0] > arr1Copy[1]) {
    arr1Copy = [arr1Copy[1], arr1Copy[0]];
  }
  if (arr2Copy[0] > arr2Copy[1]) {
    arr2Copy = [arr2Copy[1], arr2Copy[0]];
  }
  // 判断两个范围是否相交
  return arr1Copy[1] > arr2Copy[0] && arr1Copy[0] < arr2Copy[1];
}
// base64转为arraybuffer
export function base64ToAb(baseStr) {
  const binaryString = atob(baseStr);
  const { length } = binaryString;
  const bytes = new Uint8Array(length);
  for (let i = 0; i < length; i++) {
    bytes[i] = binaryString.charCodeAt(i);
  }
  return bytes.buffer;
}
// 下载二进制
export function getBlobByUrl(url) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.responseType = 'arraybuffer';
    xhr.onload = () => {
      resolve(xhr.response);
    };
    xhr.onerror = e => {
      reject(e);
    };
    xhr.send();
  });
}
function writeString(view, offset, str) {
  for (let i = 0; i < str.length; i++) {
    view.setUint8(offset + i, str.charCodeAt(i));
  }
}
// 给arraybuffer添加wav头部
export function addBufferWavHeader(data) {
  const buffer = new ArrayBuffer(Number('44') + data.byteLength);
  const view = new DataView(buffer);
  // RIFF 固定字符串
  writeString(view, 0, 'RIFF');
  // 录音数据长度+(44-8)
  view.setUint32(Number('4'), Number('36') + data.byteLength, true);
  // WAVE 固定字符串
  writeString(view, Number('8'), 'WAVE');
  // "fmt " 固定字符串
  writeString(view, Number('12'), 'fmt ');
  // size1=16 固定值
  view.setUint32(Number('16'), Number('16'), true);
  // format tag=1 固定值
  view.setUint16(Number('20'), 1, true);
  // chanel 声道数 1为单声道 2为双声道
  view.setUint16(Number('22'), 1, true);
  // sampleRate 采样率
  view.setUint32(Number('24'), Number('16000'), true);
  // byteRate 每秒所需字节数
  view.setUint32(Number('28'), Number('16000') * 1 * Number('2'), true);
  // blockAlign 每一个采样需要的字节数
  view.setUint16(Number('32'), Number('16000') * Number('2'), true);
  // bitsPerSample 采样位宽
  view.setUint16(Number('34'), Number('16'), true);
  // subchunk2ID 固定值data
  writeString(view, Number('36'), 'data');
  // subchunk2Size 音频文件总长度 文件总长度-44
  view.setUint32(Number('40'), data.byteLength, true);
  const orginData = new DataView(data);
  for (let index = 0; index < data.byteLength; index++) {
    view.setUint8(Number('44') + index, orginData.getUint8(index));
  }
  return buffer;
}
// 判断音频格式 true:就是ok的
export function judgeAudioTypeOk(audioSrc) {
  const DISABLE_MUSIC_TYPE = ['ape', 'wma'];
  const splitArr = audioSrc.split('.');
  const audioType = splitArr[splitArr.length - 1];
  return DISABLE_MUSIC_TYPE.indexOf(audioType) === -1;
}
// 用于绘制worker传递过来的数据
export function renderCanvasByOffScreen(canvas, offscreen, width, height) {
  requestAnimationFrame(() => {
    Object.assign(canvas, { width, height });
    const ctx = canvas.getContext('2d');
    ctx.clearRect(0, 0, width, height);
    ctx.drawImage(offscreen, 0, 0);
  });
}
